import { AutoCenter, DotLoading, Form, Input, Button, Toast, Modal } from "antd-mobile";
import { isValidArr, navigateTo } from "@/utils";
import ItemComp from "./item";
import React, { useEffect, useState } from "react";
import requewt from "@/utils/requewt";
import { API } from "@/config";
import callFeedback from "@/components/common/modalFeedback";

export default function () {
  const [loading, setLoading] = useState(false);
  const [data, setData] = useState(undefined);
  const [drawNum, setDrawNum] = useState(0)
  const [form] = Form.useForm()
  useEffect(() => {
    (async () => {
      await compHandler.fetch()
    })()
  }, [])

  const compHandler = {
    // 获取活动状态
    fetch: async () => {
      setLoading(true);

      const resp = await requewt(API.prize.mine);
      // const respDrawNum = await requewt(API.lottery.remain)
      // console.log(`resp`, resp)
      setLoading(false)
      if (resp?.error_code !== 0) {
        await Toast.clear();
        await callFeedback({title: resp?.msg || `请求失败`});
        return;
      }
      setData(resp?.data);
      const { 
        i_address,
        i_name,
        i_phone } = resp.data.input_info
      form.setFieldsValue({
        i_address:i_address||'',
        i_name:i_name||'' ,
        i_phone:i_phone||''  
      })
      // console.log(respDrawNum?.data,'respDrawNum?.data')
      // setDrawNum(respDrawNum?.data.remain_draw_num)
    },
    // 提交地址信息
    userAddressInfo() {
      Toast.show({
        content:'加载中',
        duration:0
      })
      requewt(API.user.userAddressInfo, {method: 'POST',headers: {"Content-Type": `multipart/form-data`},body: form.getFieldsValue()}).then(({error_code,msg}) => {
        Toast.clear()
        if (error_code === 0) {
          Toast.show('提交成功')
          Modal.clear()
        } else {
          Toast.show(msg)
        }
      }).catch(() => {
        Toast.clear()
      })
    }
  }

  return (
    <div className={`cp-prize`}>
      {/* <div className="header"> */}
        {/* 我的奖品 */}
      {/* </div> */}
      <div className="content">
      
        <div className="prizes">
        
          {
            loading ?
              <div className="p-3"><DotLoading /></div>
              :
              isValidArr(data?.gifts) ?
                <div style={{maxHeight:'48vh',overflowY:'scroll'}}>
                  {
                    data.gifts.map((item, index) => {
                      return (
                        <ItemComp item={item} key={index} />
                      )
                    })
                  }
                </div>
                :
                <div className="p-3">暂无</div>
          }

        </div>

      </div>
      <div className="footer">

        <div style={{textAlign:'left'}}>
          填写实物收货信息
        </div>
        <Form layout="horizontal" form={form}>
          <Form.Item label="姓名" name='i_name'>
            <Input />
          </Form.Item>
          <Form.Item label="电话" name="i_phone">
            <Input />
          </Form.Item>
          <Form.Item label="地址" name="i_address">
            <Input />
          </Form.Item>
        </Form>
        <AutoCenter>
          <Button shape={`rounded`}
                  loading={loading}
                  className="act-btn act-btn-bevel"
                  onClick={compHandler.userAddressInfo}
          >
            提交
          </Button>
        </AutoCenter>
        
      </div>
    </div>
  )
}